package com.toolbox.alui.portalpages.browsing.portalprefs;

import com.plumtree.openfoundation.util.*;
import com.plumtree.openlog.OpenLogService;
import com.plumtree.openlog.OpenLogger;
import com.plumtree.portaluiinfrastructure.statichelpers.PTDebugHelpers;
import com.plumtree.uiinfrastructure.activityspace.*;

/**
 * 
 *
 * @author $author$
 */
public class PortalPrefsControl implements IControl
{
	
	private static OpenLogger log = OpenLogService.GetLogger(
			OpenLogService.GetComponent(PTDebugHelpers.COMPONENT_PORTAL_BROWSING),
			"com.toolbox.alui.portalpages.browsing.portalprefs.PortalPrefsControl");
	
	public static final String STR_MVC_CLASS_NAME = "PortalPrefs";
	private static final String PARAM_ACTION = "action";
	private static final String VALUE_ACTION_SETPREFS = "setprefs";
	private static final String VALUE_ACTION_GETPREFS = "getprefs";
	
	private static final String PARAM_PREFTYPE = "type";
	private static final String VALUE_PREFTYPE_PORTLET = "portlet";
	private static final String VALUE_PREFTYPE_ADMIN = "admin";
	private static final String VALUE_PREFTYPE_SESSION = "session";
	private static final String VALUE_PREFTYPE_USER = "user";
	private static final String VALUE_PREFTYPE_COMMUNITY = "community";
	private static final String VALUE_PREFTYPE_COMMUNITYPORTLET = "communityportlet";
	
	private static final String PARAM_PORTLETID = "portletid";
	private static final String PARAM_COMMUNITYID = "commid";
	
	private static final String strTrue = "true";


	private AActivitySpace m_asasParent;
	private IPortalPrefsModel m_asmModel;

	/**
	 * @see com.plumtree.portaluiinfrastructure.activityspace.IControl#Execute()
	 */
	public Redirect CheckActionSecurityAndExecute(XPHashtable arguments)
	{
		String[] arrAction = (String[]) arguments.GetElement(PARAM_ACTION);
		String[] arrPrefType = (String[]) arguments.GetElement(PARAM_PREFTYPE);
		
		//default to session if type is not specified
		String strPrefType = VALUE_PREFTYPE_SESSION;
		if(null != arrPrefType && arrPrefType.length == 1 && arrPrefType[0] != null){
			strPrefType = arrPrefType[0];
		}
		
		log.Info("Type of preference to set is: [" + strPrefType + "]");
		
		if (null != arrAction && arrAction.length == 1 && arrAction[0] != null && null != strPrefType)
		{
			if (VALUE_ACTION_SETPREFS.compareToIgnoreCase(arrAction[0]) == 0)
			{
				if (VALUE_PREFTYPE_PORTLET.compareToIgnoreCase(strPrefType) == 0){
					int portletID = getPortletID(arguments);
					
					m_asmModel.SetPortletPrefs(arguments, portletID);
				} else if (VALUE_PREFTYPE_SESSION.compareToIgnoreCase(strPrefType) == 0){
					m_asmModel.SetSessionPrefs(arguments);
				} else if (VALUE_PREFTYPE_USER.compareToIgnoreCase(strPrefType) == 0){
					m_asmModel.SetUserPrefs(arguments);
				} else if (VALUE_PREFTYPE_COMMUNITY.compareToIgnoreCase(strPrefType) == 0){
					int commID = getCommunityID(arguments);
					
					m_asmModel.SetCommunityPrefs(arguments,commID);
				} else if (VALUE_PREFTYPE_COMMUNITYPORTLET.compareToIgnoreCase(strPrefType) == 0){
					int portletID = getPortletID(arguments);
					int commID = getCommunityID(arguments);
					
					m_asmModel.SetCommunityPortletPrefs(arguments, commID, portletID);
				} else if (VALUE_PREFTYPE_ADMIN.compareToIgnoreCase(strPrefType) == 0){
					int portletID = getPortletID(arguments);
					
					m_asmModel.SetAdminPortletPrefs(arguments, portletID);
				}
			}
			else if (VALUE_ACTION_GETPREFS.compareToIgnoreCase(arrAction[0]) == 0)
			{
				if (VALUE_PREFTYPE_PORTLET.compareToIgnoreCase(strPrefType) == 0){
					int portletID = getPortletID(arguments);
					
					m_asmModel.GetPortletPrefs(arguments, portletID);
				} else if (VALUE_PREFTYPE_SESSION.compareToIgnoreCase(strPrefType) == 0){
					m_asmModel.GetSessionPrefs(arguments);
				} else if (VALUE_PREFTYPE_USER.compareToIgnoreCase(strPrefType) == 0){
					m_asmModel.GetUserPrefs(arguments);
				} else if (VALUE_PREFTYPE_COMMUNITY.compareToIgnoreCase(strPrefType) == 0){
					int commID = getCommunityID(arguments);
					
					m_asmModel.GetCommunityPrefs(arguments,commID);
				} else if (VALUE_PREFTYPE_COMMUNITYPORTLET.compareToIgnoreCase(strPrefType) == 0){
					int portletID = getPortletID(arguments);
					int commID = getCommunityID(arguments);
					
					m_asmModel.GetCommunityPortletPrefs(arguments, commID, portletID);
				} else if (VALUE_PREFTYPE_ADMIN.compareToIgnoreCase(strPrefType) == 0){
					int portletID = getPortletID(arguments);
					
					m_asmModel.GetAdminPortletPrefs(arguments, portletID);
				}
			}
		}
		return null;
	}

	private int getCommunityID(XPHashtable arguments){
		String[] arrCommunityId = (String[]) arguments.GetElement(PARAM_COMMUNITYID);
		int commID = 0;
		if(null != arrCommunityId && arrCommunityId.length == 1 && arrCommunityId[0] != null){
			try{
				commID = Integer.parseInt(arrCommunityId[0]);
			}catch (Exception exc){
				commID = 0;
			}
		}
		
		log.Debug("getCommunityID from request: [" + commID + "]");
		return commID;
	}
	
	private int getPortletID(XPHashtable arguments){
		String[] arrPortletId = (String[]) arguments.GetElement(PARAM_PORTLETID);
		int portletID = 0;
		if(null != arrPortletId && arrPortletId.length == 1 && arrPortletId[0] != null){
			try{
				portletID = Integer.parseInt(arrPortletId[0]);
			}catch (Exception exc){
				portletID = 0;
			}
		}
		log.Debug("getPortletID from request: [" + portletID + "]");
		return portletID;
	}
	
	/**
	 * @see com.plumtree.uiinfrastructure.activityspace.IManagedObject#Create()
	 */
	public Object Create()
	{
		return new PortalPrefsControl();
	}

	/**
	 * @see com.plumtree.portaluiinfrastructure.activityspace.IControl#GetName()
	 */
	public String GetName()
	{
		return STR_MVC_CLASS_NAME;
	}

	/**
	 * @see com.plumtree.portaluiinfrastructure.activityspace.IControl#Init(IModel, AActivitySpace)
	 */
	public void Init(IModel model, AActivitySpace space)
	{
		m_asmModel		 = (IPortalPrefsModel) model;
		m_asasParent     = space;
	}
}

